<?php
namespace app\admin\controller;
use think\Db;
/**
 * 算力
 * Class NoticeController
 * Author mww
 * @package app\admin\controller
 */
class StudentController extends CommonController
{
    /**
     * 获得数据
     */
    public function getStudents()
    {
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        $_POST = $this->getDataByCampusid($_POST);
        $student = Db::table('ew_student')->alias('stu')
            ->join('ew_subject s','stu.stu_course=s.s_id','LEFT')
            ->where($_POST)
            ->order('stu_id desc')
            ->limit($rows * ($page - 1), $rows)->select();
        $total = Db::table('ew_student')->alias('stu')
            ->join('ew_subject s','stu.stu_course=s.s_id','LEFT')
            ->where($_POST)->count();
        $data['total'] = $total;
        $data['rows'] = $student;
        return json_encode($data);
    }
    /**
     * 显示修改学生页面
     */
    public function updatemodal()
    {
        $stuid = $_POST['stu_id'];
        $data = Db::table('ew_student')->where('stu_id',$stuid)->find();
        $subject = Db::table('ew_subject')->select();
        $this->assign('data',$data);
        $this->assign('subject',$subject);
        return $this->fetch("student/update");
    }
    public function update()
    {
        $stuid = trim($_POST['stu_id'],',');
        unset($_POST['stu_id']);
        foreach ($_POST as $key=>$value) {
            $value = trim($value,' ');
            if($value == ""){
                return json_encode(['status'=>0,'msg'=>'有未填项']);
            }
        }
        $res = Db::table('ew_student')->where('stu_id',$stuid)->update($_POST);
        if($res){
            $data['status'] = 200;
            $data['msg'] = '修改成功';
            return json_encode($data);
        }else{
            $data['status'] = 0;
            $data['msg'] = '修改失败或者您未做任何修改';
            return json_encode($data);
        }
    }

    /**
     * @return string
     * 查看学生详情
     */
    public function seeinfo()
    {
        $stuid = trim($_POST['stu_id'],',');
        $data = Db::table('ew_student')->alias('stu')
            ->join('ew_subject s','stu.stu_course=s.s_id','LEFT')
            ->where('stu_id',$stuid)->find();
        $teachers = array();
        if($data['stu_teachers']){
            $teachers = Db::table('ew_teacher')->where('tea_id','in',$data['stu_teachers'])->field('tea_name,tea_id')->select();
        }
        $this->assign('data',$data);
        $this->assign('teachers',$teachers);
        return $this->fetch("student/seeinfo");
    }
    /**
     * 修改此学生指定老师的模板
     */
    public function update_stu_tea_model()
    {
        $stuid = trim($_POST['stu_id'],',');
        $data = Db::table('ew_student')
            ->where('stu_id',$stuid)->field('stu_id,stu_teachers')->find();
        $myteachers = array();
        if($data['stu_teachers']){
            $myteachers = explode(',',$data['stu_teachers']);
        }
        $teachers = Db::table('ew_teacher')->field('tea_name,tea_id')->select();
        $this->assign('myteachers',$myteachers);
        $this->assign('teachers',$teachers);
        $this->assign('data',$data);
        return $this->fetch("student/update_stu_tea_model");
    }
    /**
     * 修改此学生的指定老师
     */
    public function update_stu_tea()
    {
        $stuid = trim($_POST['stu_id'],',');
        $teas = $_POST['stu_teachers'];
        $value = '';
        if($teas){
            $value1 = implode(',',$teas);
            $value = trim($value1,',');
        }
        $res = Db::table('ew_student')->where('stu_id',$stuid)->update(['stu_teachers'=>$value]);
        if($res){
            $data['status'] = 200;
            $data['msg'] = '修改成功';
            return json_encode($data);
        }else{
            $data['status'] = 0;
            $data['msg'] = '修改失败或者您未做任何修改';
            return json_encode($data);
        }
    }
    /**
     * @return string
     * 删除学生
     */
    public function deleteByID()
    {
        $id = trim($_POST['id'],',');
        //====================================================================
        Db::startTrans();
        try{
            Db::table('ew_student')->where('stu_id',$id)->delete();
            Db::table('ew_homework')->where('h_studentid',$id)->delete();
            Db::table('ew_lesson')->where('l_studentid',$id)->delete();
            // 提交事务
            Db::commit();
            return json_encode(['status'=>200,'msg'=>'操作成功']);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return json_encode(['status'=>0,'msg'=>'操作失败，请联系管理员']);
        }
    }
//====================================口语预约=========================================

    /**
     * 获得数据
     */
    public function getStudentKouyu()
    {
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        $_POST = $this->getDataByCampusid($_POST);
        if(!isset($_POST['l_date'])){
            $today = date('Y-m-d');
            $_POST['l_date'] = array('>=',$today);
        }
        $student = Db::table('ew_lesson')->alias('l')
            ->join('ew_student stu','l.l_studentid=stu.stu_id','LEFT')
            ->join('ew_teacher t','l.l_teacherid=t.tea_id','LEFT')
            ->join('ew_subject s','l.l_subjectid=s.s_id','LEFT')
            ->where($_POST)
            ->field('stu_id,stu_nickname,tea_id,tea_name,s_id,s_name,l.*')
            ->order('l_date')
            ->limit($rows * ($page - 1), $rows)->select();
        $total = Db::table('ew_lesson')->alias('l')
            ->join('ew_student stu','l.l_studentid=stu.stu_id','LEFT')
            ->join('ew_teacher t','l.l_teacherid=t.tea_id','LEFT')
            ->join('ew_subject s','stu.stu_course=s.s_id','LEFT')
            ->where($_POST)
            ->count();
        $data['total'] = $total;
        $data['rows'] = $student;
        return json_encode($data);
    }
    /**
     * 显示修改学生预约页面
     */
    public function updatelessonmodal()
    {
        $lid = $_POST['lid'];
        $data = Db::table('ew_lesson')->alias('l')
            ->join('ew_student stu','l.l_studentid=stu.stu_id','LEFT')
            ->join('ew_teacher t','l.l_teacherid=t.tea_id','LEFT')
            ->join('ew_subject s','l.l_subjectid=s.s_id','LEFT')
            ->where('l_id',$lid)
            ->field('stu_id,stu_nickname,tea_id,tea_name,s_id,s_name,l.*')
            ->find();
        //==========——start——如果是以前的记录不能修改,只能改评价=====================================
        $can = 1;
            $date = date('Y-m-d');
            $hour = date('H');
            if($date > $data['l_date']){
                $can = 0;
            }
            if($date == $data['l_date']){
                if($data['l_hour_start'] <= $hour){
                    $can = 0;
                }
            }
       //=======================——end——=========================
        $subject = Db::table('ew_subject')->select();
        $teacher = Db::table('ew_teacher')->select();
        $this->assign('data',$data);
        $this->assign('subject',$subject);
        $this->assign('teacher',$teacher);
        $this->assign('can',$can);
        return $this->fetch("student/updatelesson");
    }
    /**
     * 修改学生预约信息
     */
    public function updatelesson()
    {
        $lid = trim($_POST['l_id'],',');
        unset($_POST['l_id']);
        $_POST['l_hour_end'] = $_POST['l_hour_start']+1;
        if(isset($_POST['l_status']) && $_POST['l_status'] != 1){
            $_POST['l_stu_status'] = '';
            $_POST['l_stu_pingjia'] = '';
        }
        $res = Db::table('ew_lesson')->where('l_id',$lid)->update($_POST);
        if($res){
            $data['status'] = 200;
            $data['msg'] = '修改成功';
            return json_encode($data);
        }else{
            $data['status'] = 0;
            $data['msg'] = '修改失败或者您未做任何修改';
            return json_encode($data);
        }
    }
    /**
     * @return string
     * 删除学生预约
     */
    public function deleteLessonByID()
    {
        $lid = trim($_POST['lid'],',');
        $res = Db::table('ew_lesson')->where('l_id',$lid)->delete();
        if($res){
            return json_encode(['status'=>200,'msg'=>'操作成功']);
        }else{
            return json_encode(['status'=>0,'msg'=>'操作失败，请联系管理员']);
        }
    }
//====================================作业预约=========================================
    /**
     * 获得数据
     */
    public function gethomework()
    {
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        $_POST = $this->getDataByCampusid($_POST);
        $student = Db::table('ew_homework')->alias('h')
            ->join('ew_student stu','h.h_studentid=stu.stu_id','LEFT')
            ->join('ew_teacher t','h.h_teacherid=t.tea_id','LEFT')
            ->join('ew_subject s','h.h_subjectid=s.s_id','LEFT')
            ->where($_POST)
            ->field('stu_id,stu_nickname,tea_id,tea_name,s_id,s_name,h.*')
            ->order('h_date')
            ->limit($rows * ($page - 1), $rows)->select();
        $total = Db::table('ew_homework')->alias('h')
            ->join('ew_student stu','h.h_studentid=stu.stu_id','LEFT')
            ->join('ew_teacher t','h.h_teacherid=t.tea_id','LEFT')
            ->join('ew_subject s','h.h_subjectid=s.s_id','LEFT')
            ->where($_POST)
            ->field('h_id')
            ->count();
        $data['total'] = $total;
        $data['rows'] = $student;
        return json_encode($data);
    }
    /**
     * 显示修改预约作业页面
     */
    public function updatehomeworkmodal()
    {
        $hid = $_POST['hid'];
        $data = Db::table('ew_homework')->alias('h')
            ->join('ew_student stu','h.h_studentid=stu.stu_id','LEFT')
            ->join('ew_teacher t','h.h_teacherid=t.tea_id','LEFT')
            ->join('ew_subject s','h.h_subjectid=s.s_id','LEFT')
            ->where('h_id',$hid)
            ->field('stu_id,stu_nickname,tea_id,tea_name,s_id,s_name,h.*')
            ->find();
        //==========——start——如果是以前的记录不能修改,只能改评价=====================================
        $can = 1;
        $date = date('Y-m-d');
        if($date > $data['h_date']){
            $can = 0;
        }
        //=======================——end——=========================
        $subject = Db::table('ew_subject')->select();
        $teacher = Db::table('ew_teacher')->select();
        $this->assign('data',$data);
        $this->assign('subject',$subject);
        $this->assign('teacher',$teacher);
        $this->assign('can',$can);
        return $this->fetch("student/updatehomework");
    }
    /**
     * 修改学生预约信息
     */
    public function updatehomework()
    {
        $hid = trim($_POST['h_id'],',');
        $data = array();
        $data['h_teacherid'] = $_POST['h_teacherid'];
        $data['h_subjectid'] = $_POST['h_subjectid'];
        $res = Db::table('ew_homework')->where('h_id',$hid)->update($data);
        if($res){
            $data['status'] = 200;
            $data['msg'] = '修改成功';
            return json_encode($data);
        }else{
            $data['status'] = 0;
            $data['msg'] = '修改失败或者您未做任何修改';
            return json_encode($data);
        }
    }
    /**
     * @return string
     * 删除作业预约
     */
    public function deleteHomeworkByID()
    {
        $hid = trim($_POST['hid'],',');
        $data = array();
        $data['h_status'] = 0;
        $data['h_subjectid'] = '';
        $data['h_teacherid'] = '';
        $data['h_studentid'] = '';
        $data['h_stuurl'] = '';
        $data['h_teaurl'] = '';
        $res = Db::table('ew_homework')->where('h_id',$hid)->update($data);
        if($res){
            return json_encode(['status'=>200,'msg'=>'操作成功']);
        }else{
            return json_encode(['status'=>0,'msg'=>'操作失败，请联系管理员']);
        }
    }







    /**
     * 显示添加合同页面
     */
    public function add()
    {
        $users = Db::table('ew_user')->field('user_id,user_username')->select();
        $this->assign('users',$users);
        return $this->fetch("contract/add");
    }
     public function add_showcom()
    {
        $coms = Db::table('ew_computer')
//            ->where('com_status',1)
            ->where('com_have','>=',1)
            ->select();
        //=========筛选添加时点击分配所获得的真正可以分配的份额================
        $use_str = trim($_POST['usedata'],',');
        if($use_str != ""){
            $use_array = explode(',',$use_str);
            foreach ($coms as $k=>$v){
                foreach ($use_array as $k2=>$v2){
                    $res = explode(':',$v2);
                    if($v['com_id'] == $res[0]){
                        $coms[$k]['com_have'] = $coms[$k]['com_have'] - $res[1];
                        if($coms[$k]['com_have'] <= 0){
                            unset($coms[$k]);
                        }
                    }
                }
            }
        }
        //==============================================================
        $mymax = $_POST['mymax'];
        $this->assign('coms',$coms);
        $this->assign('mymax',$mymax);
        return $this->fetch("contract/add_showcom");
    }
    public function insert()
    {
        foreach ($_POST as $key => $value) {
            $value = trim($value,' ');
            if($value == ""){
                return json_encode(['status'=>0,'msg'=>'有未填项']);
            }
        }
        $isok = Db::table('ew_contract')->where('con_number',$_POST['con_number'])->find();
        if(!empty($isok)){
            return json_encode(['status'=>0,'msg'=>'合同编号重复']);
        }
        if($_POST['con_enddate'] <= $_POST['con_startdate']){
            return json_encode(['status'=>0,'msg'=>'结束日期应大于开始日期']);
        }else{
            $startdate=strtotime(date('Y-m-d'));
            $enddate=strtotime($_POST['con_enddate']);
            $days=round(($enddate-$startdate)/3600/24);
            $_POST['con_days'] = $days;
        }
        $_POST['con_status'] = 1;
        $_POST['con_insertdate'] = date('Y-m-d');//录入时
        $usedata = trim($_POST['usedata'],',');
        unset($_POST['usedata']);
        //====================================================================
        Db::startTrans();
        try{
            $conid = Db::name('contract')->insertGetId($_POST);
            $data = array();
            $data['cc_userid'] = $_POST['con_userid'];
            $data['cc_contractid'] = $conid;
            $use_array = explode(',',$usedata);
            foreach ($use_array as $k2=>$v2){
                $res = explode(':',$v2);
                $data['cc_computerid'] = $res[0];
                $data['cc_many'] = $res[1];
                Db::table('ew_contract_com')->insert($data);
                $lastcom = Db::table('ew_computer')->where('com_id',$res[0])->find();
                $last_use = $lastcom['com_use']+$res[1];
                $last_have = $lastcom['com_have']-$res[1];
                Db::table('ew_computer')->where('com_id',$res[0])
                    ->update(['com_use'=>$last_use,'com_have'=>$last_have]);
            }
            // 提交事务
            Db::commit();
            $u = Db::table('ew_user')->where('user_id',$_POST['con_userid'])->find();
            $this->duanxin($u['user_phone'],7,$u['user_nickname'],$_POST['con_number']);
            return json_encode(['status'=>200,'msg'=>'操作成功']);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return json_encode(['status'=>0,'msg'=>'操作失败，请联系管理员']);
        }
    }




}